package com.my.sort;

/**
 * Created by ZhongGang
 * at 2018/10/10 9:30
 * <p>
 * 堆排序
 */
public class HeapSorter<T extends Comparable<T>> implements Sortable<T> {

    @Override
    public T[] sort(T[] elements) {
        MinHeap<T> heap = new MinHeap<>(elements.length + 1);
        for (T element : elements) {
            heap.add(element);
        }

        int index = 0;
        T t;
        while ((t = heap.pop()) != null) {
            elements[index++] = t;
        }

        return elements;
    }

}
